﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBS.Model;
using IBS.ServiceContracts;

namespace IBS.Bll.Security
{
    public class SecurityService : ServiceBase, ISecurityService
    {
        public SecurityService(IIbsContext context)
            : base(context)
        {

        }
        private string GetClentUserName(Client client)
        {
            return client.Client_User.FirstOrDefault().UserName;
        }

        private Client GetClientByUserName(string userName)
        {
            var client = _context.ClientUsers.Single(c_u => c_u.UserName == userName).Client;
            return client;
        }


        public bool IsCardOwner(Card card, string userName)
        {

            return card.Client.Id == GetClientByUserName(userName).Id;
        }

        public bool IsAccountOwner(Account account, string userName)
        {
            return account.Client.Id == GetClientByUserName(userName).Id;
        }
        public bool IsPaymentOwner(Payment payment, string userName)
        {
            return payment.Client.Id == GetClientByUserName(userName).Id;
        }

        public bool IsAutoPaymentOwner(AutoPayment autoPayment, string userName)
        {
            return autoPayment.Card.Client.Id== GetClientByUserName(userName).Id;
        }
    }
}
